package com.gs.synedocket.activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.gs.synedocket.R;
import com.gs.synedocket.activity.base.BaseTouchActivity;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.util.CoordinateUtil;
import com.gs.synedocket.util.LanguageUtils;
import com.gs.synedocket.util.PermissionUtil;
import com.gs.synedocket.util.WindowUtil;

import java.util.Locale;

public class LocationActivity extends BaseTouchActivity {
    private MapView mMapView = null;
    private AMap mMap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        WindowUtil.getInstance().setWindowDecorView(this);
        mMapView = findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        initView();
    }

    private void initView() {
        mMap = mMapView.getMap();
        mMap.setMapLanguage(LanguageUtils.getInstance().getCurrentLanguage() == Locale.ENGLISH ? AMap.ENGLISH : AMap.CHINESE);
        // 隱藏指南針
        UiSettings uiSettings = mMap.getUiSettings();
        uiSettings.setCompassEnabled(false);
        setLocation();
    }

    private void setLocation() {
        String location = getIntent().getStringExtra(AllConstants.LOCATION);
        if (TextUtils.isEmpty(location)) {
            return;
        }
        String[] split = location.split("/");
        LatLng point = new LatLng(Double.parseDouble(split[0]), Double.parseDouble(split[1]));
        point = CoordinateUtil.WGSToGCJ(point);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(point);
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.location)));
        mMap.addMarker(markerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 12));
    }


    @Override
    protected void onResume() {
        if (mMapView != null) {
            mMapView.onResume();
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.onPause();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (mMapView != null) {
            mMapView.onDestroy();
        }
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (PermissionUtil.requestPermission(grantResults)) {
            initView();
        }
    }
}